package okna;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.prefs.NodeChangeEvent;
import java.util.prefs.NodeChangeListener;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.JTextPane;
import javax.swing.JList;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;

public class Informacje extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTree tree;
	private JTextPane textPane;
	private static Pattern pat = Pattern.compile(".*#id=.*#.*");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Informacje dialog = new Informacje();
			
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Informacje() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 700, 400);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		textPane = new JTextPane();
		textPane.setContentType("text/html");
		textPane.setEditable(false);
		JScrollPane scroll = new JScrollPane(textPane);
		scroll.setBounds(12, 12, 430, 312);
		contentPanel.add(scroll);
		
		tree = new JTree();
		tree.setModel(new DefaultTreeModel(
			new DefaultMutableTreeNode("Info") {
				{
					DefaultMutableTreeNode node_1;
					node_1 = new DefaultMutableTreeNode("autorzy");
						appendChild(node_1, getFilesNames("resources/informacje/autorzy"));
					add(node_1);
					node_1 = new DefaultMutableTreeNode("program");
					appendChild(node_1, getFilesNames("resources/informacje/program"));
					add(node_1);
					node_1 = new DefaultMutableTreeNode("tryby");
					appendChild(node_1, getFilesNames("resources/informacje/tryby"));
					add(node_1);
				}
			}
		));
		tree.setBounds(454, 12, 200, 312);
		tree.addTreeSelectionListener(new NodeListener());
		contentPanel.add(tree);
		Zakoncz zak = new Zakoncz();
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				okButton.addActionListener(zak);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(zak);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
	}
	
	private void appendChild(DefaultMutableTreeNode parrent,String[] childs){
		for(int i =0; i < childs.length; i++)
			parrent.add(new DefaultMutableTreeNode(childs[i].replace(".html", "")));
	}
	
	private String[] getFilesNames(String folder){
		String[] files = null;
		File fol = new File(folder);
		if(fol.isDirectory()){
			files = fol.list();
		}
		return files;
	}
	public void close(){
		WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
	}
	
	public String loadFile(String file){
		StringBuilder text = new StringBuilder();
		
		File f = new File("resources/informacje/" + file + ".html");
		
		try {
			Scanner in = new Scanner(f);
			while(in.hasNextLine()){
				text.append(in.nextLine());
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			return "Brak pliku";
		}
		
		return text.toString();
	}
	
	class Zakoncz implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			JButton s = (JButton)e.getSource();
			close();
		}
		
	}
	
	class NodeListener implements TreeSelectionListener{

		@Override
		public void valueChanged(TreeSelectionEvent e) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                    tree.getLastSelectedPathComponent();
			if(node != null && node.isLeaf()){
				textPane.setText(loadFile(node.getParent().toString() + "/" +  node.toString()));
				//textPane.insertIcon(new ImageIcon ( "resources/gui/dice.png" ));
			}
			
		}

	}
}

